using UnityEngine;
using System.Collections;

public class ColorPickerHandler : MonoBehaviour {
	
	public GameObject primary;
	public GameObject secondary;
	
	public GameObject objectToColor;
	public GameObject laserToColor;
	public GameObject colorIcon;
	
	public bool hasParticles = false;
		
	private Color currentColor = Color.white;
	
	
	void Start () {
	
		
		if(!LevelConstants.allowPrimary){
			primary.SetActive(false);
		}
		
		if(!LevelConstants.allowSecondary){
			secondary.SetActive(false);
		}
		
	}
	
	public void colorObject(Color c){
		currentColor = c;
		
		if(objectToColor.renderer != null){
			objectToColor.renderer.material.SetColor("_Color",c);
		}
		
		foreach(Transform child in objectToColor.transform){
			if(child.renderer != null){
				float alpha = child.renderer.material.GetColor("_Color").a;
				c.a = alpha;
				child.renderer.material.SetColor("_Color",c);
			}
		}
		
		if(laserToColor != null){
			c.a = 1f;
			laserToColor.GetComponent<LightRay>().initColor = c;	
		}
		
		if(hasParticles){
			c.a = 1f;
			if(objectToColor.particleSystem != null){
				objectToColor.particleSystem.startColor = c;	
			}
		}
			
		if(transform.parent.GetComponent<LightRayHandler>() != null){
			transform.parent.GetComponent<LightRayHandler>().currentColor = c;
		}
		
		gameObject.SetActive(false);
		if(colorIcon != null){
			if(transform.parent.GetComponent<LightRayHandler>() != null){
				if(!transform.parent.GetComponent<LightRayHandler>().isFixed){
					colorIcon.SetActive(true);
				}
			}
			
		}
	}

	void LateUpdate(){
	    transform.rotation = Quaternion.identity;
		
		if(!LevelConstants.allowPrimary){
			primary.SetActive(false);
		}
		
		if(!LevelConstants.allowSecondary){
			secondary.SetActive(false);
		}
	}
	
	
	public Color CurrentColor
	{
	    get
	    {
	        return currentColor;
	    }
	}
}
